//
//  KFDChatListViewController.m
//  KFD
//
//  Created by Lucky Luke on 8/16/15.
//  Copyright (c) 2015 kfd. All rights reserved.
//

#import "KFDChatListViewController.h"
#import "KFDClient.h"
#import "JTToast.h"
#import "KFDUIConfigurator.h"
#import "ui_config.h"
#import "KFDChatCell.h"
#import "KFDChatViewController.h"

@interface KFDChatListViewController ()

@property (strong,nonatomic) NSArray* chatList;

@end

@implementation KFDChatListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"一对一聊天";
    self.tableView.backgroundColor=KFD_BG_COLOR;
    KFDClient* client=[KFDClient sharedClient];
    [client getChatsWithSuccessHandler:^(NSArray *chats) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.chatList=chats;
            [self.tableView reloadData];
        });
    } failureHandler:^(KFDResponseBody *response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
        });
    }];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//----------------------------------------------------------------------
#pragma mark - Table view data source
//----------------------------------------------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.chatList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KFDChatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KFDChatCell"];
    if (!cell) {
        cell=[[NSBundle mainBundle] loadNibNamed:@"KFDChatCell" owner:nil options:nil].firstObject;
    }
    cell.chat=self.chatList[indexPath.row];
    return cell;
}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
#pragma mark - Table view delegate
//----------------------------------------------------------------------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KFDChatViewController* chatVC=[[KFDChatViewController alloc] init];
    chatVC.chat=self.chatList[indexPath.row];
    [self.navigationController pushViewController:chatVC animated:YES];
}
//----------------------------------------------------------------------

@end
